package com.thinkgem.jeesite.common.web;

import com.thinkgem.jeesite.common.exception.MobileApiException;
import com.thinkgem.jeesite.common.exception.UserAuthorizationException;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.Map;

/**
 * Created by admin on 2017-12-5.
 */

public abstract class BaseMobileApiController {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @ResponseBody
    @ResponseStatus(value = HttpStatus.UNAUTHORIZED)  // 401
    @ExceptionHandler(UserAuthorizationException.class)
    public Map<String, Object> userAuthorizationException(UserAuthorizationException e) {
        logger.error(e.getMessage(), e);
        Map<String, Object> map = new HashedMap();
        map.put("errMsg", e.getMessage());
        return map;
    }

    @ResponseBody
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)  // 400
    @ExceptionHandler(MobileApiException.class)
    public Map<String, Object> mobileApiException(MobileApiException e) {
        logger.error(e.getMessage(), e);
        Map<String, Object> map = new HashedMap();
        map.put("errMsg", e.getMessage());
        return map;
    }

    @ResponseBody
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)  // 500
    @ExceptionHandler(Exception.class)
    public Map<String, Object> exception(Exception e) {
        logger.error(e.getMessage(), e);
        Map<String, Object> map = new HashedMap();
        map.put("errMsg", "Internal Server Error");
        map.put("detailMsg", e.getMessage());
        return map;
    }

}
